package com.geekyouup.android.animtimer;

import com.geekyouup.android.animtimer.StopwatchView.StopwatchThead;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class AnimTimer extends Activity implements OnClickListener {
	
	private TextView mTextView;
	private static final String PREFS_NAME="USTOPWATCH_PREFS";
	
	private StopwatchView mStopWatchView;
	private StopwatchThead mWatchThread;
	private PowerManager mPowerMan;
	private PowerManager.WakeLock mWakeLock; 
	private ImageView mResetBtn;
	
	private static final int MENU_STARTPAUSE = 0;
	private static final int MENU_EXIT = 3;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPowerMan = (PowerManager) getSystemService(Context.POWER_SERVICE);
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        
        setContentView(R.layout.main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
        mStopWatchView = (StopwatchView) findViewById(R.id.swview);
        mTextView = (TextView) findViewById(R.id.text);
        mResetBtn = (ImageView) findViewById(R.id.resetButton);
        mResetBtn.setOnClickListener(this);
        
        mWatchThread = mStopWatchView.getThread();
        mWatchThread.setApplication(this);

        //if vars stored then use them
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        if(settings != null && settings.contains("state"))
        {
        	removeSplashText();
        	mWatchThread.restoreState(settings);
        }
        
        mWakeLock = mPowerMan.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "Stopwatch");
        mWakeLock.acquire();
     }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        
        menu.add(0,MENU_STARTPAUSE,0,"Start/Pause").setIcon(R.drawable.play_pause);
        menu.add(0, MENU_EXIT, 5, "Exit").setIcon(android.R.drawable.ic_lock_power_off );

        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	if(item.getItemId() == MENU_STARTPAUSE)
    	{
    		mWatchThread.goPauseUnpause();
    	}else if(item.getItemId() == MENU_EXIT)
    	{
    		try{getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().clear().commit();}catch(Exception e){}
			finish();
    	}
    	
    	return true;
    }
    
	public void onClick(View v) {
		removeSplashText();
		if(v==mResetBtn)
		{
			mWatchThread.reset();
		}
	}
	
	public void removeSplashText()
	{
		mTextView.setVisibility(View.GONE);
	}
	
	/*** Capture Back Button and use for browser back, else quit ****/
	public boolean onKeyDown(int keyCode, KeyEvent event) 
	{
		if(keyCode == KeyEvent.KEYCODE_BACK)
		{
			try{getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().clear().commit();}catch(Exception e){}
			return super.onKeyDown(keyCode, event);
		}else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP)
		{
        	try{((AudioManager)getSystemService(Context.AUDIO_SERVICE)).adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);}catch(Exception e){}
			return true;
		}else if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
		{
        	try{((AudioManager)getSystemService(Context.AUDIO_SERVICE)).adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);}catch(Exception e){}
			return true;
		}else //not back button or no history to go back to
		{
			return super.onKeyDown(keyCode, event);
		}
	}

	@Override
    protected void onPause() {
        super.onPause();

        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        if(settings != null)
        {
	        SharedPreferences.Editor editor = settings.edit();
	        if(editor != null)
	        {
		        mWatchThread.saveState(editor);
		        editor.commit();
	        }
        }

        mStopWatchView.getThread().pause(); // pause when Activity pauses
        mWakeLock.release();
    }
}